// 02-03-refactor-average.js:   使用帮忙函数_average重构averageDollarValue，使用函数组合的方式实现

const fp = require('lodash/fp');
const cars = require('./02-help-file');

let _average = function (xs) {//无须改动
    return fp.reduce(fp.add, 0, xs) / xs.length
}

//改造前：
let averageDollatValue = function (cars) {
    let dollar_values = fp.map(function (car) {
        return car.dollar_value;
    }, cars);
    return _average(dollar_values);//790700
}

console.log(averageDollatValue(cars));

/**
 * 思路：
 * 1.使用fp.map()方法遍历(以'dollar_value做为key)，生成数组
 * 2.将数组传递给_average方法求平均值
 */

//改造后：
let averageDollatValue_1 = fp.flowRight(_average, fp.map(fp.prop('dollar_value')));

console.log(averageDollatValue_1(cars));//790700
